#!/usr/bin/perl

use strict;

package Lunmgr::MSA;

use XML::Twig;
use Data::Dumper;
use Carp;
our $AUTOLOAD;

my $_lunmgr_home = "/home/lunmgr";

sub new
{
    my ($proto, $msaname) = @_;

    my $class = ref($proto) || $proto;

    my $self = { _name => $msaname };

    bless $self, $class;

    $self->__load("$_lunmgr_home/msa_xml/$msaname.xml") ? $self : undef;

}

sub AUTOLOAD
{
    my $self = $_[0];

    my $attr = $AUTOLOAD;
    $attr =~ s/.*:://;

    return $self->{"_$attr"} if exists $self->{"_$attr"};

    carp "Invalid call for $AUTOLOAD";
    undef;
}

sub __load
{
    my ($self, $file) = @_;

    return undef unless (-f $file);

    my $twig = XML::Twig->new();
    $twig->parsefile($file);
    my $twig_root = $twig->root;

    $self->{_ips} = [split(',', $twig_root->att('ipaddrs'))];

    my @controllers = $twig_root->children('controllers');

    foreach my $controller ($controllers[0]->children) {
        my @models = $controller->children('model');
        my $model = $models[0]->children_text;
        $model =~ s/ /_/g;

        my @ports = $controller->children('ports');
        foreach ($ports[0]->children) {
            push @{$self->{_wwns}}, $_->att('wwn');
            $self->{_models}{$_->att('wwn')} = $model;
        }
    }

    $self;
}

sub DESTROY {}

sub ips { $_[0]->{_ips}; }
sub wwns { @{$_[0]->{_wwns}}; }

use overload q("") => sub {
        my $self = $_[0];
        
    };
1;
